package com.zyy.smartcity1129.lifepay;

import android.graphics.Color;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.zyy.smartcity1129.BaseFragment;
import com.zyy.smartcity1129.R;
import com.zyy.smartcity1129.util.PpUtil;
import com.zyy.smartcity1129.util.RvAd;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: LPAccount$
 * @Description: 作用
 * @Author: letquit
 * @CreateDate: 7/12/2021$
 * @Version: 1.0
 */
public class LPAccountFragment extends BaseFragment {
    private Button btnAddList;
    private RecyclerView rvLPAList;
    RvAd<String> rvAd;
    private FrameLayout frameP1;
    private TextView tvP1;
    private TextView tvP2;
    private TextView tvP3;
    private TextView tvP4;
    private EditText editP5;
    private Button btnP6;
    private FrameLayout frameP2;
    String choose = "";

    @Override
    protected int layId() {
        return R.layout.f_lpaccount;
    }

    @Override
    protected String title() {
        return null;
    }

    @Override
    protected boolean back() {
        return false;
    }

    @Override
    protected Boolean bottom() {
        return null;
    }

    @Override
    protected void init(View iv) {
        LifePayFragment.mutableLiveData.postValue("户号管理");
        initView(iv);
        List<String> list = new ArrayList<>();
        rvLPAList.setLayoutManager(new LinearLayoutManager(getContext()));
        btnAddList.setOnClickListener(view -> {
            PpUtil ppUtil = new PpUtil();
            View view1 = ppUtil.getP(requireContext(), R.layout.p_listsort);
            frameP1 = (FrameLayout) view1.findViewById(R.id.frame_p1);
            tvP1 = (TextView) view1.findViewById(R.id.tvP1);
            tvP2 = (TextView) view1.findViewById(R.id.tvP2);
            tvP3 = (TextView) view1.findViewById(R.id.tvP3);
            tvP4 = (TextView) view1.findViewById(R.id.tvP4);
            editP5 = (EditText) view1.findViewById(R.id.editP5);
            btnP6 = (Button) view1.findViewById(R.id.btnP6);
            frameP2 = (FrameLayout) view1.findViewById(R.id.frame_p2);
            tvP1.setOnClickListener(view2 -> {
                choose = tvP1.getText().toString();
                tvP1.setTextColor(ContextCompat.getColor(requireContext(), R.color.black));
                tvP2.setTextColor(ContextCompat.getColor(requireContext(), R.color.gray));
                tvP3.setTextColor(ContextCompat.getColor(requireContext(), R.color.gray));
                tvP4.setTextColor(ContextCompat.getColor(requireContext(), R.color.gray));
                editP5.setText("");
            });
            tvP2.setOnClickListener(view2 -> {
                choose = tvP2.getText().toString();
                tvP2.setTextColor(ContextCompat.getColor(requireContext(), R.color.black));
                tvP1.setTextColor(ContextCompat.getColor(requireContext(), R.color.gray));
                tvP3.setTextColor(ContextCompat.getColor(requireContext(), R.color.gray));
                tvP4.setTextColor(ContextCompat.getColor(requireContext(), R.color.gray));
                editP5.setText("");
            });
            tvP3.setOnClickListener(view2 -> {
                choose = tvP3.getText().toString();
                tvP3.setTextColor(ContextCompat.getColor(requireContext(), R.color.black));
                tvP4.setTextColor(ContextCompat.getColor(requireContext(), R.color.gray));
                tvP1.setTextColor(ContextCompat.getColor(requireContext(), R.color.gray));
                tvP2.setTextColor(ContextCompat.getColor(requireContext(), R.color.gray));
                editP5.setText("");
            });
            tvP4.setOnClickListener(view2 -> {
                choose = tvP4.getText().toString();
                tvP4.setTextColor(ContextCompat.getColor(requireContext(), R.color.black));
                tvP1.setTextColor(ContextCompat.getColor(requireContext(), R.color.gray));
                tvP2.setTextColor(ContextCompat.getColor(requireContext(), R.color.gray));
                tvP3.setTextColor(ContextCompat.getColor(requireContext(), R.color.gray));
                editP5.setText("");
            });
            editP5.setOnEditorActionListener(new TextView.OnEditorActionListener() {
                @Override
                public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
                    choose = editP5.getText().toString();
                    tvP4.setTextColor(Color.parseColor("#9E9797"));
                    tvP1.setTextColor(ContextCompat.getColor(requireContext(), R.color.gray));
                    tvP2.setTextColor(ContextCompat.getColor(requireContext(), R.color.gray));
                    tvP3.setTextColor(ContextCompat.getColor(requireContext(), R.color.gray));
                    return false;
                }
            });
            btnP6.setOnClickListener(view2 -> {
                if (!choose.equals("")) {
                    list.add(choose);
//                    Log.i("TAG", choose);
//                    Log.i("TAG", String.valueOf(list.size()));
//                    存在传入数组但rv没刷新
                    initAdapter(list);
                    Log.i("TAG1", String.valueOf(rvAd.getItemCount()));
                    rvAd.notifyDataSetChanged();
                    rvLPAList.setAdapter(rvAd);
                    ppUtil.closeP();
                } else {
                    Toast.makeText(getContext(), "请选择分类", Toast.LENGTH_SHORT).show();
                }

            });
            frameP1.setOnClickListener(view2 -> {
                ppUtil.closeP();
            });
            frameP2.setOnClickListener(view2 -> {
                ppUtil.closeP();
            });
        });
    }

    private void initAdapter(List<String> list) {
        rvAd = new RvAd<String>(list, R.layout.r_account) {
            private FrameLayout framePpp2;
            private Button btnPpp6;
            private EditText editppp;
            private Spinner spppp;
            private FrameLayout framePpp1;

            private void initView(View iv) {
                tv = (TextView) iv.findViewById(R.id.tv);
                rv = (RecyclerView) iv.findViewById(R.id.rv);
                btn = (Button) iv.findViewById(R.id.btn);
                rv.setVisibility(View.GONE);
                btn.setVisibility(View.GONE);
            }

            private Button btn;
            private RecyclerView rv;
            private TextView tv;

            @Override
            protected void initR(View vi, String s, int p) {
                initView(vi);
                List<String> sList = new ArrayList<>();
                rv.setLayoutManager(new LinearLayoutManager(getContext()));
                tv.setText(s);
                tv.setOnClickListener(view -> {
                    rv.setVisibility(View.VISIBLE);
                    btn.setVisibility(View.VISIBLE);
                });
                RvAd<String> rvAd1 = new RvAd<String>(sList, R.layout.rv_text) {
                    private void initView(View vi) {
                        tv = (TextView) vi.findViewById(R.id.tv);
                    }

                    private TextView tv;

                    @Override
                    protected void initR(View vi, String s, int p) {
                        initView(vi);
                        tv.setText(s);
                    }
                };
                btn.setOnClickListener(view -> {
                    PpUtil ppUtil = new PpUtil();
                    View view1 = ppUtil.getP(requireContext(), R.layout.p_account);
                    framePpp1 = (FrameLayout) view1.findViewById(R.id.frame_ppp1);
                    spppp = (Spinner) view1.findViewById(R.id.spppp);
                    editppp = (EditText) view1.findViewById(R.id.editppp);
                    btnPpp6 = (Button) view1.findViewById(R.id.btnPpp6);
                    framePpp2 = (FrameLayout) view1.findViewById(R.id.frame_ppp2);
                    btnPpp6.setOnClickListener(view2 -> {
                        String sp = spppp.getSelectedItem().toString();
                        String edit = editppp.getText().toString().trim();
                        if (sp.equals("") || edit.equals("")) {
                            Toast.makeText(getContext(), "请完整输入", Toast.LENGTH_SHORT).show();
                        } else {
                            String s1 = sp + " " + edit;
                            sList.add(s1);
                            rv.setAdapter(rvAd1);
                            ppUtil.closeP();
                        }
                    });
                    framePpp1.setOnClickListener(view2 -> {
                        ppUtil.closeP();
                    });
                    framePpp2.setOnClickListener(view2 -> {
                        ppUtil.closeP();
                    });
                });
            }
        };
    }

    private void initView(View iv) {
        btnAddList = (Button) iv.findViewById(R.id.btnAddList);
        rvLPAList = (RecyclerView) iv.findViewById(R.id.rvLPAList);
    }
}
